package com.atguigu.crowd.mvc.config;

import com.atguigu.crowd.exception.AccessForbiddenException;
import com.atguigu.crowd.exception.IllegalOperationException;
import com.atguigu.crowd.exception.LoginAcctAlreadyUseException;
import com.atguigu.crowd.exception.LoginFailedException;
import com.atguigu.crowd.util.CrowdConstant;
import com.atguigu.crowd.util.CrowdUtil;
import com.atguigu.crowd.util.ResultEntity;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@ControllerAdvice
public class CrowdExceptionResolver {

    @ExceptionHandler(LoginFailedException.class)
    public ModelAndView loginFailedException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
        return commonResolveException(request, response, e, "admin-login");
    }

    @ExceptionHandler(LoginAcctAlreadyUseException.class)
    public ModelAndView loginAcctAlreadyUseException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
        return commonResolveException(request, response, e, "admin-authority-user-add");
    }


    @ExceptionHandler(AccessForbiddenException.class)
    public String accessForbiddenException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
        return "redirect:/admin/login";
    }

    @ResponseBody
    @ExceptionHandler(IllegalOperationException.class)
    public ResultEntity<String> illegalOperationException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
        return ResultEntity.failed(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
        return commonResolveException(request, response, e, "system-error");
    }

    private ModelAndView commonResolveException(HttpServletRequest request, HttpServletResponse response,
                                                Exception e, String viewName) throws IOException {
        if (CrowdUtil.isAjaxRequest(request)) {
            Gson gson = new Gson();
            String json = gson.toJson(ResultEntity.failed(e.getMessage()));
            PrintWriter writer = response.getWriter();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            writer.write(json);
            return null;
        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject(CrowdConstant.ATTR_NAME_EXCEPTION, e);
            modelAndView.setViewName(viewName);

            return modelAndView;
        }
    }
}
